define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'categorys/index/index' + location.search,
                    add_url: 'categorys/index/add',
                    edit_url: 'categorys/index/edit',
                    del_url: 'categorys/index/del',
                    multi_url: 'categorys/index/multi',
                    import_url: 'categorys/index/import',
                    table: 'categorys',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                escape:false,
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'pid', title: __('Pid')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', align: 'left',table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'level', title: __('等级'), operate: false},
                        {field: 'user_id', title: __('管理'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('上传'),
                                    title: __('上传文件'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-arrow-up',
                                    url: 'categorys/index/addfile',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        console.log(row)
                                        if (row.level=='车间级'){

                                            return true;
                                        }else{
                                            return false;
                                        }


                                    }
                                }]

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        addfile: function () {
            //只需要添加这行代码，就可调用上传组件
            Controller.api.bindevent();

            require(['upload'], function(Upload){
                Upload.api.plupload();
            });
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
